using UnityEngine;
using System.Collections;

/// \class  gkUtilityVertices
/// \brief  Mesh vertices functions
public class gkUtilityVertices
{
	/// \brief  Compute the verices bounding box
	static public Bounds ComputeVerticesBounds(Vector3[] a_rVertices)
	{
		Bounds oBounds;
		Vector3 f3Min;
		Vector3 f3Max;
		Vector3 fSize;
		
		// initialize
		f3Min = Vector3.zero;
		f3Max = Vector3.zero;
		
		/// loop through the vertices and find the min and max on each axis
		foreach(Vector3 f3VertexPosition in a_rVertices)
		{
			// Update the min
			f3Min.x = Mathf.Min(f3VertexPosition.x, f3Min.x);
			f3Min.y = Mathf.Min(f3VertexPosition.y, f3Min.y);
			f3Min.z = Mathf.Min(f3VertexPosition.z, f3Min.z);
			
			// Update the max
			f3Max.x = Mathf.Max(f3VertexPosition.x, f3Max.x);
			f3Max.y = Mathf.Max(f3VertexPosition.y, f3Max.y);
			f3Max.z = Mathf.Max(f3VertexPosition.z, f3Max.z);
		}
		
		// Compute the size
		fSize = f3Max - f3Min;
		
		// Create the bounds
		oBounds = new Bounds(fSize * 0.5f + f3Min,
		                     fSize);
		
		return oBounds;
	} 
	
	/// \brief  Compute the verices barycenter
	static public Vector3 ComputeVerticesBarycenter(Vector3[] a_rVertices)
	{
		Vector3 f3Barycenter;
		int iNbVertices;
		
		// Initialize the barycenter
		f3Barycenter = Vector3.zero;
		
		// If there is at least one vertex
		iNbVertices = a_rVertices.Length;
		if(iNbVertices > 0)
		{
			/// loop through the vertices to compute their sum
			foreach(Vector3 f3VertexPosition in a_rVertices)
			{
				f3Barycenter += f3VertexPosition;
			}
			
			// Divide the sum by the number of vertices
			f3Barycenter /= iNbVertices;
		}
		
		return f3Barycenter;
	}
}
